const App = getApp();
Component({
    options: {
        multipleSlots: true, // 在组建定义时的选项中启用多slot支持
    },
    /**
     * 组件的属性列表
     */
    properties: {
        url: {
            type: String,
            value: true
        },
        pageSize: {
            type: Number,
            value: 0
        }
    },
    /**组件所在页面的生命周期声明对象 */
    pageLifetimes: {
        show() { // 页面被展示
            this.setData({
                url: this.properties.url,
                pageSize: this.properties.pageSize,
            })
        },
    },
    ready() {
        console.log("loadmore component ready")
        this.methods.loadMore();
    },
    /**
     * 组件的初始数据
     */
    data: {
        hasMore:true,
        page: 1
    },
    /**自定义方法 */
    methods: {
        /**开始滑动 */
        touchstart(e) {
            /**记录开始滑动的时间 */
            this.setData({
                slideStart: e.touches[0]
            })
        },
        /**滑动 */
        touchmove(e) {
            let moveTime = new Date().getTime(); // 当前时间
            if (moveTime - this.data.moveTime <= 2000) {
                return
            } else {
                this.setData({
                    moveTime: moveTime
                })
            }
            let slideStart = this.data.slideStart;
            let slideMove = e.touches[0];
            let startX = slideStart.pageX;
            let startY = slideStart.pageY;
            let moveEndX = slideMove.pageX;
            let moveEndY = slideMove.pageY;
            let X = moveEndX - startX;
            let Y = moveEndY - startY;
            if (Math.abs(Y) > Math.abs(X) && Y > 0) { // 从上向下滑
                console.log("top 2 bottom");
                this.pullRefresh()
            } else if (Math.abs(Y) > Math.abs(X) && Y < 0) { // 从下向上滑
                console.log("bottom 2 top");
                this.loadMore()
            }
        },
        /**上拉加载更多 */
        loadMore() {
            let _this  = this;
            // if (this.data.hasMore) {
                let page = this.data.page + 1;
                this.setData({"page": page})
                App._get(this.data.url, this.data.data, function (result) {
                    if(result.rows.length<_this.data.pageSize){
                        _this.setData({hasMore: false})
                    } else {
                        this.triggerEvent('onrender', result)
                    }
                });
            // }
        }
    }
})